package cn.nexd.app.collector.presenter.settinglocation;

import android.content.Context;
import android.text.TextUtils;

import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;

import java.io.File;
import java.util.List;

import cn.nexd.app.collector.Configuration;
import cn.nexd.app.collector.presenter.network.HttpMethod;
import cn.nexd.app.collector.util.UrlUtil;
import cn.nexd.utils.framework.tools.FileUtil;
import cn.nexd.utils.framework.tools.SDCardUtil;

/**
 * Created by wangxu on 16/12/1.
 */

public class SettingLocationPresenter implements SettingLocationContract.Presenter {


    private Context context;

    private SettingLocationContract.View iView;
    private Acp acp;

    public SettingLocationPresenter(Context context, SettingLocationContract.View iView) {
        this.context = context;
        this.iView = iView;
        this.acp = Acp.getInstance(context);
    }

    @Override
    public void init() {

    }


    @Override
    public void checkPermission() {
        AcpOptions.Builder builder = new AcpOptions.Builder();
        builder.setPermissions(Configuration.READ_EXTERNAL_STORAGE,
                Configuration.WRITE_EXTERNAL_STORAGE,
                Configuration.ACCESS_COARSE_LOCATION,
                Configuration.ACCESS_FINE_LOCATION);
        acp.request(builder.build(), new AcpListener() {
            @Override
            public void onGranted() {
                iView.showDownDialog();
            }

            @Override
            public void onDenied(List<String> permissions) {
                iView.showToast(permissions.toString() + "被拒绝");
            }
        });
    }

    @Override
    public void requestUrl(final String mallId) {

        iView.showLoadingDialog();

        String builder = UrlUtil.BASE_IP +
                "nexd/v1.0/getBuildingPropUrl.do?buildingCode=" + mallId + "&appKey=f4b5aeaa1776f7af714e0663fa2e50b2";

        HttpMethod.getInstance(context).execute(builder, new HttpMethod.OnRequestListener<String>() {
            @Override
            public void success(String data) throws Exception {

                if (TextUtils.isEmpty(data)) {
                    iView.showToast("数据异常，请联系管理员");
                } else {
                    downLoad(data, mallId);
                }
                iView.dismissLoadingDialog();
            }

            @Override
            public void requestError(String msg) {
                iView.dismissLoadingDialog();
                iView.showToast(msg);
            }

            @Override
            public void networkError(String msg) {
                iView.dismissLoadingDialog();
                iView.showToast(msg);
            }
        });
    }

    private void downLoad(String url, String buildingId) {
        iView.showLoadingDialog();
        final String zipFilePath = SDCardUtil.getSDCardPath() + File.separator + "nexdLocation/location/download/" + buildingId + ".zip";

        HttpMethod.getInstance(context).downLoad(url, zipFilePath, new HttpMethod.OnRequestListener<Boolean>() {
            @Override
            public void success(Boolean data) throws Exception {
                if (data) {
                    FileUtil.unCompressZipFile(zipFilePath, SDCardUtil.getSDCardPath() + "/nexdLocation/location/");
                    FileUtil.deleteFile(zipFilePath.substring(0, zipFilePath.lastIndexOf("/")));
                    iView.startActivity();
                } else {
                    iView.showToast("数据下载失败");
                }
                iView.dismissLoadingDialog();
            }

            @Override
            public void requestError(String msg) {
                iView.showToast(msg);
                iView.dismissLoadingDialog();
            }

            @Override
            public void networkError(String msg) {
                iView.showToast(msg);
                iView.dismissLoadingDialog();
            }
        });
    }
}
